<template>
  <div>
    <div class="department-container">
      <div class="app-container">
        <el-card>
          <el-tabs v-model="activeName">
            <el-tab-pane label="角色管理" name="first" />
            <el-button type="primary" @click="add">+新增角色</el-button>
            <el-dialog :title="isEdit ? '编辑':'添加'" :visible.sync="showDialog" @close="dialogClose">
              <el-form
                ref="roleForm"
                :model="roleForm"
                :rules="rules"
                label-width="100px"
              >
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="roleForm.name" />
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                  <el-input v-model="roleForm.description" />
                </el-form-item>
              </el-form>
              <!-- 底部 -->
              <el-row slot="footer" type="flex" justify="center">
                <el-col :span="6">
                  <el-button size="small" @click="showDialog=false">取消</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="sure"
                  >确定</el-button>
                </el-col>
              </el-row>
            </el-dialog>
            <el-table
              ref="singleTable"
              :data="tableData"
              highlight-current-row
              style="width: 100%"
            >
              <el-table-column type="index" label="序号" width="50" />
              <el-table-column property="name" label="职位" width="120" />
              <el-table-column property="description" label="介绍" />
              <el-table-column label="操作" width="300">
                <template slot-scope="scoped">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editFn(scoped.row)">编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delFn(scoped.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tabs>
          <el-pagination
            background
            layout="prev, pager, next"
            :page-size="params.pagesize"
            :total="total"
            @current-change="currentChangeFn"
          />
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { getRoles, deleteRole, addRole, updateRole } from '@/api/setting'
export default {
  data() {
    return {
      activeName: 'first',
      tableData: [],
      total: 0, // 总数据
      params: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      showDialog: false, // 弹窗是否显示
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述内容不能为空', trigger: 'blur' }]
      },
      isEdit: false
    }
  },
  created() {
    this.getRolesFn()
  },
  methods: {
    async getRolesFn() {
      const res = await getRoles(this.params)
      console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // 点击页码发送请求
    currentChangeFn(page) {
      // 拿到当前页的页码
      this.params.page = page
      // 再去发送请求
      this.getRolesFn()
    },
    // 点击新增按钮
    add() {
      this.showDialog = true
      this.isEdit = false
    },
    // 新增数据请求
    async addFn() {
      const res = await addRole(this.roleForm)
      console.log('新增按钮', res)
      this.showDialog = false
    },
    // 删除按钮
    delFn(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        // 删除操作成功
        // 拿到id 发送请求

        if (this.tableData.length === 1) {
          this.params.page = (this.params.page - 1) || 1
        }
        await deleteRole(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.getRolesFn()
      }).catch(error => {
        console.log(error)
      })
    },
    // 点击确定
    sure() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) {
          return
        }
        this.isEdit ? this.edit() : this.addFn()
        this.getRolesFn()
      })
    },
    // 编辑的请求
    async edit() {
      const res = await updateRole(this.roleForm)
      console.log(res)
      this.showDialog = false
    },
    // 点击编辑按钮
    editFn({ id, name, description }) {
      this.showDialog = true
      this.roleForm = { id, name, description }
      this.isEdit = true
    },
    dialogClose() {
      this.roleForm.name = ''
      this.roleForm.description = ''
      this.$refs.roleForm.clearValidate()
    }
  }

}
</script>

<style>
</style>
